package ig.metrica.ejb.conflujos.dao;


import ig.da.abs.AbstractBasicDao;
import java.sql.SQLException;
import java.lang.Exception;

import ig.metrica.ejb.conflujos.ConfiguracionbalancesDTO;
import ig.metrica.ejb.conflujos.ConfiguracionbalancesPK;
import ig.util.ejb.IGBean;

/**
 * GenJ 2011
 * @author usuario
 *
 * Is an Class for table DB for DAO
 */
public class ConfiguracionbalancesDAO extends AbstractBasicDao{

	// -----------------------------------------------------------------
	// Constants
	// -----------------------------------------------------------------

	// -----------------------------------------------------------------
	// Attributes
	// -----------------------------------------------------------------
    private java.sql.Connection connection = null;

	// -----------------------------------------------------------------
	// Constructors
	// -----------------------------------------------------------------
    public ConfiguracionbalancesDAO ()  {

    }

	// -----------------------------------------------------------------
	// All Methods
	// -----------------------------------------------------------------

                                                        

    /**
     * Method for create an record of table of table
     * @return : the pk of table
     * @param dto
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public  ig.metrica.ejb.conflujos.ConfiguracionbalancesPK  create(ConfiguracionbalancesDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;

        try{
            String QUERY = "INSERT INTO CONFIGURACIONBALANCES ( IDMODELO, PUC, DURACION, PROCENTAJE ) VALUES ( ?,  ?,  ?,  ? )";

            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getIdmodelo());
            prep.setInt (2,dto.getPuc());
            prep.setInt (3,dto.getDuracion());
            prep.setInt (4,dto.getProcentaje());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return dto.getPk();
    }


    /**
     * Update by Pk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int update (ConfiguracionbalancesDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE CONFIGURACIONBALANCES SET , , WHERE IDMODELO = ? AND PUC = ? AND DURACION = ? AND PROCENTAJE = ?";
            prep = connection.prepareStatement(QUERY);

            prep.setInt (1,dto.getIdmodelo());
            prep.setInt (2,dto.getPuc());
            prep.setInt (3,dto.getDuracion());
            prep.setInt (4,dto.getProcentaje());
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Delete by Pk
     * @param pk
     * @return the number record delete
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int delete (ConfiguracionbalancesPK pk ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM CONFIGURACIONBALANCES WHERE IDMODELO = ? AND PUC = ? AND DURACION = ? AND PROCENTAJE = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,pk .getIdmodelo());
            prep.setInt (2,pk .getPuc());
            prep.setInt (3,pk .getDuracion());
            prep.setInt (4,pk .getProcentaje());
            
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Find by Pk
     * @param pk
     * @return DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public ConfiguracionbalancesDTO findByPk (ConfiguracionbalancesPK pk ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        ConfiguracionbalancesDTO configuracionbalances = null;
        try{
            String QUERY = "SELECT IDMODELO, PUC, DURACION, PROCENTAJE FROM CONFIGURACIONBALANCES WHERE IDMODELO = ? AND PUC = ? AND DURACION = ? AND PROCENTAJE = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,pk .getIdmodelo());
            prep.setInt (2,pk .getPuc());
            prep.setInt (3,pk .getDuracion());
            prep.setInt (4,pk .getProcentaje());
            res = prep.executeQuery();
            if (res.next()) {
                configuracionbalances = new ConfiguracionbalancesDTO(  res.getInt ( 1 ), res.getInt ( 2 ), res.getInt ( 3 ), res.getInt ( 4 ));
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,res,connection);
        }
        return configuracionbalances;
    }




    public java.util.List< ig.metrica.ejb.conflujos.ConfiguracionbalancesPK > createAll(java.util.List<ConfiguracionbalancesDTO> dtos) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;
        java.util.List< ig.metrica.ejb.conflujos.ConfiguracionbalancesPK > list=new java.util.ArrayList< ig.metrica.ejb.conflujos.ConfiguracionbalancesPK >();
        try{
            String QUERY = "INSERT INTO CONFIGURACIONBALANCES ( IDMODELO, PUC, DURACION, PROCENTAJE ) VALUES ( ?,  ?,  ?,  ? )";
            for(ConfiguracionbalancesDTO dto : dtos ){

                prep = connection.prepareStatement(QUERY);
                prep.setInt (1,dto.getIdmodelo());
                prep.setInt (2,dto.getPuc());
                prep.setInt (3,dto.getDuracion());
                prep.setInt (4,dto.getProcentaje());
                prep.executeUpdate();
                list.add(dto.getPk());
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return list;
    }


    /**
     * The List By idmodelo
     * @param idmodelo
     * @return list of ConfiguracionbalancesDTO
     */
    public java.util.List<ConfiguracionbalancesDTO> findByIdmodelo (Integer  idmodelo) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionbalancesDTO> list=new java.util.ArrayList<ConfiguracionbalancesDTO>();

        try{
            String QUERY = "SELECT IDMODELO, PUC, DURACION, PROCENTAJE FROM CONFIGURACIONBALANCES WHERE IDMODELO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,idmodelo);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionbalancesDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By puc
     * @param puc
     * @return list of ConfiguracionbalancesDTO
     */
    public java.util.List<ConfiguracionbalancesDTO> findByPuc (Integer  puc) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionbalancesDTO> list=new java.util.ArrayList<ConfiguracionbalancesDTO>();

        try{
            String QUERY = "SELECT IDMODELO, PUC, DURACION, PROCENTAJE FROM CONFIGURACIONBALANCES WHERE PUC = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,puc);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionbalancesDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By duracion
     * @param duracion
     * @return list of ConfiguracionbalancesDTO
     */
    public java.util.List<ConfiguracionbalancesDTO> findByDuracion (Integer  duracion) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionbalancesDTO> list=new java.util.ArrayList<ConfiguracionbalancesDTO>();

        try{
            String QUERY = "SELECT IDMODELO, PUC, DURACION, PROCENTAJE FROM CONFIGURACIONBALANCES WHERE DURACION = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,duracion);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionbalancesDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By procentaje
     * @param procentaje
     * @return list of ConfiguracionbalancesDTO
     */
    public java.util.List<ConfiguracionbalancesDTO> findByProcentaje (Integer  procentaje) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionbalancesDTO> list=new java.util.ArrayList<ConfiguracionbalancesDTO>();

        try{
            String QUERY = "SELECT IDMODELO, PUC, DURACION, PROCENTAJE FROM CONFIGURACIONBALANCES WHERE PROCENTAJE = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,procentaje);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionbalancesDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }





    /**
     * Open the connection
     * @return
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private java.sql.Connection open() throws ClassNotFoundException, java.sql.SQLException,Exception {
    	return IGBean.getMetricaDW();//getConnection("METRICADW");
    }



}
